home *** CD-ROM | disk | FTP | other *** search
- Subject: v15i024: Filter to eliminate file inclusion commands
- Newsgroups: comp.sources.unix
- Sender: sources
- Approved: rsalz@uunet.UU.NET
-
- Submitted-by: Sten Folkerman <sten@enea.se>
- Posting-number: Volume 15, Issue 24
- Archive-name: inc-elim
-
- [ This is a general filter to remove "include" commands from files.
- It can process ".so" commands for *roff, "#include" commands for
- C, etc. It understands ~name and | for piped commands.
-
- The Makefile will cause problems for folks without the SystemV
- "augmented make", so use the Make.Alt that I wrote.
-
- The manual page uses the "C" font; if you don't have it
- you might need to do some tweaking -- check out all \f
- commands.
-
- -r$ ]
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of shell archive."
- # Contents: Make.alt Makefile file1 file2 inc-elim inelim.1net
- # inelim.c unenv.c
- # Wrapped by rsalz@fig.bbn.com on Tue May 31 16:14:00 1988
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'Make.alt' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'Make.alt'\"
- else
- echo shar: Extracting \"'Make.alt'\" \(737 characters\)
- sed "s/^X//" >'Make.alt' <<'END_OF_FILE'
- X# Makefile for inelim
- X
- X
- BINDIR= /usr/local/bin
- X
- SYSTEM=BSD
- CFLAGS = -O -D$(SYSTEM)
- X
- COMMAND= inelim
- SRCS= inelim.c unenv.c
- OBJS= inelim.o unenv.o
- MANPAGE= inelim.1net
- X
- X
- trap: btest
- X
- btest: inelim
- X ./inelim file1
- X ./inelim -v file1
- X ./inelim -p .so file1
- X ./inelim file1 | ./inelim -p source
- X
- install: $(BINDIR)/$(COMMAND)
- X
- X$(BINDIR)/$(COMMAND): $(COMMAND)
- X cp $(COMMAND) $@
- X chmod 755 $@
- X
- bsd:
- X $(MAKE) $(COMMAND) SYSTEM=BSD
- X
- att:
- X $(MAKE) $(COMMAND) SYSTEM=SYSV
- X
- X$(COMMAND): $(OBJS)
- X $(CC) $(LDFLAGS) -o $@ $(OBJS)
- X
- lint:
- X $(LINT) -D$(SYSTEM) $(LFLAGS) $(SRCS)
- X
- clean:
- X rm -f $(OBJS)
- X
- rogshar:
- X shar -c -f inelim.shar $(MANPAGE) Makefile $(SRCS) file1 file2
- X
- gosshar:
- X shar -c $(MANPAGE) Makefile $(SRCS) file1 file2 > incelim.shar
- END_OF_FILE
- if test 737 -ne `wc -c <'Make.alt'`; then
- echo shar: \"'Make.alt'\" unpacked with wrong size!
- fi
- # end of 'Make.alt'
- fi
- if test -f 'Makefile' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'Makefile'\"
- else
- echo shar: Extracting \"'Makefile'\" \(794 characters\)
- sed "s/^X//" >'Makefile' <<'END_OF_FILE'
- X# Makefile for inelim
- X
- BINDIR= /usr/local/bin
- X
- X.SUFFIXES:
- X.SUFFIXES: .none
- X
- SYSTEM=BSD
- CFLAGS = -O -D$(SYSTEM)
- X
- COMMAND= inelim
- SRCS= inelim.c unenv.c
- MANPAGE= inelim.1net
- X
- OBJS= $(SRCS:.c=.o)
- X
- trap: btest
- X
- btest: inelim
- X ./inelim file1
- X ./inelim -v file1
- X ./inelim -p .so file1
- X ./inelim file1 | ./inelim -p source
- X
- install: $(BINDIR)/$(COMMAND)
- X
- X$(BINDIR)/$(COMMAND): $$(@F)
- X cp $(@F) $@
- X chmod 755 $@
- X
- bsd:
- X $(MAKE) $(COMMAND) SYSTEM=BSD
- X
- att:
- X $(MAKE) $(COMMAND) SYSTEM=SYSV
- X
- X$(COMMAND): $(OBJS)
- X $(CC) $(LDFLAGS) -o $@ $(OBJS)
- X
- X$(OBJS): $$*.c
- X $(CC) $(CFLAGS) -c $*.c
- X
- lint:
- X $(LINT) -D$(SYSTEM) $(LFLAGS) $(SRCS)
- X
- clean:
- X rm -f $(OBJS)
- X
- rogshar:
- X shar -c -f inelim.shar $(MANPAGE) Makefile $(SRCS) file1 file2
- X
- gosshar:
- X shar -c $(MANPAGE) Makefile $(SRCS) file1 file2 > incelim.shar
- END_OF_FILE
- if test 794 -ne `wc -c <'Makefile'`; then
- echo shar: \"'Makefile'\" unpacked with wrong size!
- fi
- # end of 'Makefile'
- fi
- if test -f 'file1' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'file1'\"
- else
- echo shar: Extracting \"'file1'\" \(121 characters\)
- sed "s/^X//" >'file1' <<'END_OF_FILE'
- knatte
- include file2
- X.so file2
- include $HOME/.cshrc
- tjatte
- include | awk 'BEGIN{FS=":"}{print $1}' /etc/passwd | fmt
- end
- END_OF_FILE
- if test 121 -ne `wc -c <'file1'`; then
- echo shar: \"'file1'\" unpacked with wrong size!
- fi
- # end of 'file1'
- fi
- if test -f 'file2' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'file2'\"
- else
- echo shar: Extracting \"'file2'\" \(7 characters\)
- sed "s/^X//" >'file2' <<'END_OF_FILE'
- fnatte
- END_OF_FILE
- if test 7 -ne `wc -c <'file2'`; then
- echo shar: \"'file2'\" unpacked with wrong size!
- fi
- # end of 'file2'
- fi
- if test -f 'inc-elim' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'inc-elim'\"
- else
- echo shar: Extracting \"'inc-elim'\" \(705 characters\)
- sed "s/^X//" >'inc-elim' <<'END_OF_FILE'
- XFrom enea!sten@uunet.uu.net Mon Feb 29 03:03:05 1988
- Received: from BBN.COM (bbn.com.ARPA) by pineapple.bbn.com id AA12423; Mon, 29 Feb 88 02:56:24 est
- Received: from uunet.uu.net by BBN.COM id aa22315; 29 Feb 88 2:55 EST
- Received: from enea.UUCP by uunet.UU.NET (5.54/1.14) with UUCP
- X id AA22935; Mon, 29 Feb 88 01:48:53 EST
- Received: by enea.se (5.57++/1.19)
- X id AA12730; Mon, 29 Feb 88 00:59:08 +0100 (MET)
- Date: Mon, 29 Feb 88 00:59:08 +0100
- XFrom: Sten Folkerman <sten@enea.se>
- Message-Id: <8802282359.AA12730@enea.se>
- To: comp-sources-unix@enea.se
- Status: R
- X
- writting ./inelim.1net
- writting ./Makefile
- writting ./inelim.c
- writting ./unenv.c
- writting ./file1
- writting ./file2
- XFinished archive 1 of 1
- END_OF_FILE
- if test 705 -ne `wc -c <'inc-elim'`; then
- echo shar: \"'inc-elim'\" unpacked with wrong size!
- fi
- # end of 'inc-elim'
- fi
- if test -f 'inelim.1net' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'inelim.1net'\"
- else
- echo shar: Extracting \"'inelim.1net'\" \(1571 characters\)
- sed "s/^X//" >'inelim.1net' <<'END_OF_FILE'
- X.de r$
- X.ds rV \\$3
- X.ds rD \\$4
- X..
- X.r$ $Header: inelim.1net,v 1.3 1988-02-23 16:00:12 sten Exp $
- X.TH INELIM 1net "ENEA DATA \*(rD"
- X.ds TD \fCinelim\fR
- X.SH NAME
- inelim \- eliminate include statements
- X.SH SYNOPSIS
- X.B inelim
- X[
- X.B \-v
- X] [
- X.BI \-p pattern
- X] [
- X.I files
- X]
- X.SH DESCRIPTION
- X\*(TD reads the specified files or the standard input and performs the
- implied textual inclusion. The include statement has as default the format:
- X.PP
- X\fCinclude \fIsomefile\fR
- X.PP
- when they appear in beginning of input lines. Before \fIsomefile\fR is
- accessed, the file name will go through tilde and environment substitution,
- in a manner like \fCcsh(1)\fR. If the file name does not begin with '/',
- the include file is opened relative to the directory location of the current
- file. If the file name begins with '|', the rest of the line is fed to
- X\fCsh(1)\fR, and data from stdout is substituted in place of the include
- line. If The file name is just '-', the stdin will be read for data to
- substitute the inclusion line with.
- X.PP
- The command line options are:
- X.TP 2c
- X.BI \-p pattern
- X.I pattern
- will be used instead of \fCinclude\fR to detect include statements.
- Subsequent whitespace is skipped and the rest of the line is taken as the
- name of the file to include.
- X.TP
- X.B \-v
- verbose operation. Each line on the standard output is preceded by the input
- file name and line number.
- X.SH ENVIRONMENT
- All environment variables may be substituted into an inclusion file name.
- X.SH "SEE ALSO"
- X\fCsh(1)\fR and \fCcsh(1)\fR
- X.SH AUTHOR
- Sten Folkerman (sten@enea)
- X.SH BUGS
- No support for quotes.
- END_OF_FILE
- if test 1571 -ne `wc -c <'inelim.1net'`; then
- echo shar: \"'inelim.1net'\" unpacked with wrong size!
- fi
- # end of 'inelim.1net'
- fi
- if test -f 'inelim.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'inelim.c'\"
- else
- echo shar: Extracting \"'inelim.c'\" \(6571 characters\)
- sed "s/^X//" >'inelim.c' <<'END_OF_FILE'
- X#ifndef lint
- static char *RCSid = "$Header: inelim.c,v 1.5 1988-02-29 00:39:38 sten Exp $";
- X#endif
- X
- X/*****************************************************************************\
- X* *
- X* NAME *
- X* inelim - eliminate include statements *
- X* *
- X* SYNOPSIS *
- X* inelim [-v] [-p pattern] [files] *
- X* *
- X* DESCRIPTION *
- X* inelim eliminate include statements by substituting the line with the *
- X* include statement with the contents of then file to be included. The *
- X* result is written to stdout. The -v option prints file namn and line *
- X* number at the beginning of each line. The -p option allows you to *
- X* specify an alternative pattern instead of the standard 'include'. If no *
- X* files are given, stdin is taken. Tilde and environment substitution is *
- X* performed on each include file name. If the expanded file name doesn't *
- X* begin with a '/', the directory in which we are reading from at the *
- X* moment is prefixed. If the first character in the file name is '|', the *
- X* rest is fed to "/bin/sh" and data from stdout replaces the include *
- X* line. *
- X* *
- X* AUTHOR *
- X* sten@enea *
- X* *
- X\*****************************************************************************/
- X
- X/*
- X * $Log: inelim.c,v $
- X *
- X * Revision 1.5 1988-02-29 00:39:38 sten
- X * before public release
- X *
- X * Revision 1.4 1988-02-23 14:54:58 sten
- X * incelim => inelim
- X *
- X * Revision 1.3 1987-03-02 00:41:42 sten
- X * inclusion of command stdout.
- X *
- X * Revision 1.2 1986-10-20 16:40:57 sten
- X * released to the Inner Circle
- X *
- X * Revision 1.1 1986-10-05 23:09:39 sten
- X * Initial revision
- X */
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <assert.h>
- X
- X#ifdef BSD
- X#include <sys/param.h>
- X#endif
- X#ifdef V7
- X#define MAXPATHLEN 256
- X#endif
- X#ifdef SYSV
- X#include <limits.h>
- X#define MAXPATHLEN PATH_MAX
- X#endif
- X
- X#ifdef SYSV
- X#define rindex strrchr
- X#endif
- X
- X#define LLEN 256
- X
- extern char *strcat();
- extern char *strcpy();
- extern FILE *popen();
- extern int rindex();
- extern void exit();
- X
- X#define Strcpy (void) strcpy
- X#define Strcat (void) strcat
- X
- X#define INCLUDE "include"
- X
- char *progname;
- int verbose = 0;
- char *pattern = INCLUDE;
- X
- X/******************************************************************\
- X* *
- X* pipefile: read the file given as parameter and invoke pipefile *
- X* recursively on detected include files. *
- X* *
- X\******************************************************************/
- static void
- pipefile(cfd, cwd, cfn)
- XFILE *cfd;
- char *cwd, *cfn;
- X{
- X int lcnt = 0;
- X char iwd[MAXPATHLEN];
- X char line[LLEN];
- X
- X while (fgets(line, LLEN - 1, cfd) != 0)
- X {
- X lcnt++;
- X if (strncmp(line, pattern, strlen(pattern)) != 0)
- X {
- X if (verbose)
- X fprintf(stdout, "%s/%s, line %d: ", cwd, cfn, lcnt);
- X fputs(line, stdout);
- X } else
- X {
- X FILE *ifd;
- X char *ifn, *p;
- X char path[MAXPATHLEN];
- X char ppath[MAXPATHLEN];
- X
- X for (p = line + strlen(pattern); isspace(*p); p++);
- X if (untilde(ppath, p) != 0)
- X {
- X fprintf(stderr, "%s/%s, line %d: %s: %s\n",
- X cwd, cfn, lcnt, progname, "Cannot untilde");
- X exit(1);
- X }
- X if (ppath[0] == '|')
- X {
- X ppath[strlen(ppath) - 1] = '\0';
- X for (p = ppath + 1; isspace(*p); p++);
- X if ((ifd = popen(p, "r")) == (FILE *) NULL)
- X {
- X fprintf(stderr, "%s: Cannot popen %s\n", progname, ppath);
- X exit(1);
- X }
- X pipefile(ifd, cwd, ppath);
- X (void) pclose(ifd);
- X } else
- X {
- X if ((unenv(path, ppath)) != 0)
- X {
- X fprintf(stderr, "%s/%s, line %d: %s: %s\n",
- X cwd, cfn, lcnt, progname, "Cannot unenv");
- X exit(1);
- X }
- X if (path[0] == '/')
- X Strcpy(iwd, path);
- X else
- X {
- X Strcpy(iwd, cwd);
- X Strcat(iwd, "/");
- X Strcat(iwd, path);
- X }
- X iwd[strlen(iwd) - 1] = '\0';
- X if ((ifd = fopen(iwd, "r")) == (FILE *) NULL)
- X {
- X fprintf(stderr, "%s: Cannot open %s\n", progname, iwd);
- X exit(1);
- X }
- X ifn = (char *) rindex(iwd, '/');
- X *ifn++ = '\0';
- X pipefile(ifd, iwd, ifn);
- X (void) fclose(ifd);
- X }
- X }
- X }
- X}
- X
- X#ifdef V7
- X/**********************************************\
- X* *
- X* mygetwd retrieves current working directory *
- X* *
- X\**********************************************/
- static void
- mygetwd(to)
- char *to;
- X{
- X FILE *pwdfd;
- X char *i;
- X
- X pwdfd = popen ("/bin/pwd\n");
- X assert (pwdfd != NULL);
- X i = fgets (to, MAXPATHLEN, pwdfd);
- X assert (i != NULL);
- X pclose (pwdfd);
- X to[strlen (to) - 1] = '\0';
- X}
- X#endif
- X/*****************************************\
- X* *
- X* usage: display correct usage and exit *
- X* *
- X\*****************************************/
- static void
- usage()
- X{
- X fprintf(stderr, "Usage: %s [-v] [-p pattern] [files]\n", progname);
- X exit(1);
- X}
- X
- void
- main (argc, argv)
- int argc;
- char *argv[];
- X{
- X char sw;
- X extern int optind;
- X extern char *optarg;
- X char dir[MAXPATHLEN];
- X
- X if ((progname = (char *)rindex(argv[0], '/')) == 0)
- X progname = argv[0];
- X while ((sw = getopt(argc, argv, "vp:")) != (char) EOF)
- X switch (sw)
- X {
- X case 'v':
- X verbose++;
- X break;
- X case 'p':
- X pattern = optarg;
- X break;
- X case '?':
- X usage();
- X break;
- X }
- X#ifdef BSD
- X (void) getwd(dir);
- X#endif
- X#ifdef V7
- X mygetwd(dir);
- X#endif
- X#ifdef SYSV
- X (void) getcwd(dir, MAXPATHLEN);
- X#endif
- X if (optind == argc)
- X pipefile(stdin, dir, "");
- X else
- X for (; optind < argc; optind++)
- X {
- X FILE *fd;
- X char *file;
- X char path[MAXPATHLEN];
- X char ppath[MAXPATHLEN];
- X
- X if (strcmp(argv[optind], "-") == 0)
- X pipefile(stdin, dir, "");
- X else
- X {
- X if (untilde(ppath, argv[optind]) != 0)
- X {
- X fprintf(stderr, "%s: %s: %s\n",
- X progname, "Cannot untilde", argv[optind]);
- X exit(1);
- X }
- X if ((unenv(path, ppath)) != 0)
- X {
- X fprintf(stderr, "%s: %s: %s\n",
- X progname, "Cannot unenv", argv[optind]);
- X exit(1);
- X }
- X if (path[0] == '/')
- X Strcpy(dir, path);
- X else
- X {
- X if (dir[strlen(dir) - 1] != '/')
- X Strcat(dir, "/");
- X Strcat(dir, path);
- X }
- X if ((fd = fopen(dir, "r")) == (FILE *) NULL)
- X {
- X fprintf(stderr, "%s: Cannot open %s\n", progname, dir);
- X exit(1);
- X }
- X file = (char *) rindex(dir, '/');
- X *file++ = '\0';
- X pipefile(fd, dir, file);
- X (void) fclose(fd);
- X }
- X }
- X exit(0);
- X /* NOTREACHED */
- X}
- END_OF_FILE
- if test 6571 -ne `wc -c <'inelim.c'`; then
- echo shar: \"'inelim.c'\" unpacked with wrong size!
- fi
- # end of 'inelim.c'
- fi
- if test -f 'unenv.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'unenv.c'\"
- else
- echo shar: Extracting \"'unenv.c'\" \(1692 characters\)
- sed "s/^X//" >'unenv.c' <<'END_OF_FILE'
- X/* LINTLIBRARY */
- X
- X#ifndef lint
- static char *RCSid = "$Header: unenv.c,v 1.1 1988-02-29 00:41:23 sten Exp $";
- X#endif
- X
- X/*
- X * $Log: unenv.c,v $
- X *
- X * Revision 1.1 1988-02-29 00:41:23 sten
- X * Initial revision
- X *
- X * Revision 1.1 1986-10-20 16:51:22 sten
- X * Initial revisi
- X */
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <pwd.h>
- X#define Strcpy (void) strcpy
- X#define Strcat (void) strcat
- X
- X/****************************************\
- X* *
- X* untilde: perform tilde substitution. *
- X* *
- X\****************************************/
- X
- untilde(out, in)
- char *out, *in;
- X{
- X if (in[0] != '~')
- X {
- X Strcpy(out, in);
- X return (0);
- X }
- X if (isalnum(in[1]))
- X {
- X char user[80];
- X struct passwd *pw, *getpwnam();
- X char *p, *pu;
- X
- X p = in + 1;
- X pu = user;
- X while (isalnum(*p) || *p == '-')
- X *pu++ = *p++;
- X *pu = '\0';
- X if ((pw = getpwnam(user)) == NULL)
- X return (1);
- X Strcpy(out, pw->pw_dir);
- X Strcat(out, p);
- X } else
- X {
- X char *p;
- X
- X if ((p = (char *) getenv("HOME")) == (char *) NULL)
- X return (1);
- X Strcpy(out, p);
- X Strcat(out, in + 1);
- X }
- X return (0);
- X}
- X
- X/*******************************************\
- X* *
- X* unenv: perform environment substitution *
- X* *
- X\*******************************************/
- X
- unenv(out, in)
- char *out, *in;
- X{
- X while (*in != '\0')
- X {
- X if (*in != '$')
- X *out++ = *in++;
- X else
- X {
- X char envar[80];
- X char *p;
- X
- X in++;
- X for (p = envar; isalnum(*in);)
- X *p++ = *in++;
- X *p = '\0';
- X if ((p = (char *) getenv(envar)) == (char *) NULL)
- X return (1);
- X Strcpy(out, p);
- X out += strlen(out);
- X }
- X }
- X *out = '\0';
- X return (0);
- X}
- END_OF_FILE
- if test 1692 -ne `wc -c <'unenv.c'`; then
- echo shar: \"'unenv.c'\" unpacked with wrong size!
- fi
- # end of 'unenv.c'
- fi
- echo shar: End of shell archive.
- exit 0
-